package com.sirun.cn.fca.module.main.home.temp.control;

import com.sirun.cn.fca.module.main.home.temp.control.constant.ControlActionConst;
import com.sirun.cn.fca.module.main.home.temp.repo.ControlPollingResponse;
import com.sirun.cn.fca.module.main.home.temp.repo.ControlRequest;
import com.sirun.cn.fca.module.main.home.temp.repo.ControlResponse;
import com.tencent.cloud.iov.flow.presenter.impl.BaseFlowPresenter;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.rx.SimpleObserver;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.subjects.BehaviorSubject;

public class ControlPresenter extends BaseFlowPresenter<String, ControlLoader>
        implements ControlContract.Presenter {
    private BehaviorSubject<BaseResponse> mBehaviorSubject = BehaviorSubject.create();

    @Override
    protected ControlLoader createLoader() {
        return new ControlLoader();
    }

    @Override
    protected Observer<String> createObserver() {
        return new ControlObserver(getActionListener());
    }

    @Override
    public void controlDoor(String din, ControlRequest request) {
        getLoader().controlDoor(din, request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<ControlResponse>>() {
                    @Override
                    public void onNext(BaseResponse<ControlResponse> controlResponse) {
                        super.onNext(controlResponse);
                        ControlPresenter.this.getActionListener().onAction(ControlActionConst
                                .Normal.ACTION_CONTROL_DOOR_RESULT, controlResponse);
                    }
                });
    }


    @Override
    public void controlEngine(String din, ControlRequest request) {
        getLoader().controlEngine(din, request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<ControlResponse>>() {
                    @Override
                    public void onNext(BaseResponse<ControlResponse> controlResponse) {
                        super.onNext(controlResponse);
                        ControlPresenter.this.getActionListener().onAction(ControlActionConst
                                .Normal.ACTION_CONTROL_ENGINE_RESULT, controlResponse);
                    }
                });
    }

    @Override
    public void controlBeer(String din, ControlRequest request) {
        getLoader().controlBeer(din, request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<ControlResponse>>() {
                    @Override
                    public void onNext(BaseResponse<ControlResponse> controlResponse) {
                        super.onNext(controlResponse);
                        ControlPresenter.this.getActionListener().onAction(ControlActionConst
                                .Normal.ACTION_CONTROL_BEER_RESULT, controlResponse);
                    }
                });
    }

    @Override
    public void controlCar(String type, String din, ControlRequest request) {
        getLoader().controlCar(type, din, request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<ControlResponse>>() {
                    @Override
                    public void onNext(BaseResponse<ControlResponse> controlResponse) {
                        super.onNext(controlResponse);
                        ControlPresenter.this.getActionListener().onAction(ControlActionConst
                                .Normal.ACTION_CONTROL_CAR_RESULT, controlResponse);
                    }
                });
    }

    @Override
    public void controlPolling(String din, String platformResponseId) {
        getLoader().controlPolling(din, platformResponseId)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<ControlPollingResponse>>() {
                    @Override
                    public void onNext(BaseResponse<ControlPollingResponse> controlResponse) {
                        super.onNext(controlResponse);
                        ControlPresenter.this.getActionListener().onAction(ControlActionConst
                                .Normal.ACTION_CONTROL_POLLING_RESULT, controlResponse);
                    }
                });
    }


}
